package ${packagePrefix}.domain.service;

import ${packagePrefix}.domain.event.${domainObjectName}Event;
import ${packagePrefix}.domain.event.${domainObjectName}Event.${domainObjectName}EventType;
import ${packagePrefix}.domain.gateway.${domainObjectName}Repository;
import ${packagePrefix}.domain.model.${tableName?lower_case}.${domainObjectName};
import ${packagePrefix}.domain.model.${tableName?lower_case}.valueobject.${domainObjectName}Status;
import ${packagePrefix}.domain.shared.DomainEventPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * ${entityComment}领域服务
 * 领域服务用于处理：
 * 1. 涉及多个领域对象的业务逻辑
 * 2. 不适合放在单个领域对象中的行为
 */
@Service
public class ${domainObjectName}DomainService {

    @Autowired
    private ${domainObjectName}Repository ${domainObjectName?uncap_first}Repository;
    
    @Autowired
    private DomainEventPublisher eventPublisher;
    
    /**
     * 根据ID查询${entityComment}
     *
     * @param id ${entityComment}ID
     * @return ${entityComment}对象
     */
    public Optional<${domainObjectName}> findById(Long id) {
        return ${domainObjectName?uncap_first}Repository.findById(id);
    }
    
    /**
     * 创建${entityComment}
     *
     * @param ${domainObjectName?uncap_first} ${entityComment}对象
     * @return 创建的${entityComment}ID
     */
    public Long create${domainObjectName}(${domainObjectName} ${domainObjectName?uncap_first}) {
        // 业务规则验证
        ${domainObjectName?uncap_first}.checkValid();
        
        // 设置初始状态
        ${domainObjectName?uncap_first}.setStatus(${domainObjectName}Status.ACTIVE.getCode());
        
        // 保存实体
        Long ${domainObjectName?uncap_first}Id = ${domainObjectName?uncap_first}Repository.save(${domainObjectName?uncap_first});
        
        // 发布领域事件
        eventPublisher.publish(new ${domainObjectName}Event(${domainObjectName?uncap_first}Id, ${domainObjectName}EventType.CREATED));
        
        return ${domainObjectName?uncap_first}Id;
    }
    
    /**
     * 更新${entityComment}
     *
     * @param ${domainObjectName?uncap_first} ${entityComment}对象
     */
    public void update${domainObjectName}(${domainObjectName} ${domainObjectName?uncap_first}) {
        // 业务规则验证
        ${domainObjectName?uncap_first}.checkValid();
        
        // 保存实体
        ${domainObjectName?uncap_first}Repository.save(${domainObjectName?uncap_first});
        
        // 发布领域事件
        eventPublisher.publish(new ${domainObjectName}Event(${domainObjectName?uncap_first}.getId(), ${domainObjectName}EventType.UPDATED));
    }
    
    /**
     * 删除${entityComment}
     *
     * @param id ${entityComment}ID
     */
    public void delete${domainObjectName}(Long id) {
        // 查询实体
        Optional<${domainObjectName}> ${domainObjectName?uncap_first}Optional = ${domainObjectName?uncap_first}Repository.findById(id);
        if (${domainObjectName?uncap_first}Optional.isPresent()) {
            ${domainObjectName} ${domainObjectName?uncap_first} = ${domainObjectName?uncap_first}Optional.get();
            
            // 设置状态为删除
            ${domainObjectName?uncap_first}.setStatus(${domainObjectName}Status.DELETED.getCode());
            ${domainObjectName?uncap_first}Repository.save(${domainObjectName?uncap_first});
            
            // 发布领域事件
            eventPublisher.publish(new ${domainObjectName}Event(id, ${domainObjectName}EventType.DELETED));
        }
    }
    
    /**
     * 批量处理${entityComment}
     *
     * @param ids ${entityComment}ID列表
     * @return 处理结果
     */
    public boolean batchProcess(List<Long> ids) {
        // 这里实现涉及多个实体的复杂业务逻辑
        boolean allSuccess = true;
        
        for (Long id : ids) {
            Optional<${domainObjectName}> ${domainObjectName?uncap_first}Optional = ${domainObjectName?uncap_first}Repository.findById(id);
            if (${domainObjectName?uncap_first}Optional.isPresent()) {
                ${domainObjectName} ${domainObjectName?uncap_first} = ${domainObjectName?uncap_first}Optional.get();
                
                // 执行业务逻辑
                try {
                    processEntity(${domainObjectName?uncap_first});
                } catch (Exception e) {
                    allSuccess = false;
                }
            }
        }
        
        return allSuccess;
    }
    
    /**
     * 处理单个实体
     */
    private void processEntity(${domainObjectName} ${domainObjectName?uncap_first}) {
        // 处理实体的业务逻辑
        
        // 保存实体
        ${domainObjectName?uncap_first}Repository.save(${domainObjectName?uncap_first});
        
        // 发布领域事件
        eventPublisher.publish(new ${domainObjectName}Event(${domainObjectName?uncap_first}.getId(), ${domainObjectName}EventType.UPDATED));
    }
} 